package cn.itsource.org.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺接口
 *    crud
 *    分页+高级
 */
@RestController //@Controller+@ResponseBody
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    //添加或修改-差异在于是否有id put
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        //通过id判断是添加或者修改
        try {
            if (shop.getId()!=null){
                shopService.update(shop);
            }else {
                shopService.add(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //Get http://localhost:8080/deparment/1 deparment/2
    public Shop getById(@PathVariable("id") Long id){
        return shopService.getById(id);
    }

    //获取所有 get
    @GetMapping ///shop
    public List<Shop> getAll(){
        return shopService.getAll();
    }

    //分页+高级查询 //post /shop 传递对象都往@RequestBody
    @PostMapping
    public PageList<Shop> list(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    //入驻请求
    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
             shopService.settledIn(shop);
            return AjaxResult.me();
        } catch (BusinessException e) //业务异常捕获
        {
            return AjaxResult.me().setMessage("入驻失败!"+e.getMessage());
        }
        catch (Exception e) { //500
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常!"+e.getMessage());
        }
    }
}
